/*
* @(#)StepInformationWindow.java
*
* Copyright (c) 2004 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.workflow.applet.editor;

/**
  WorkflowErrorConstant.java

  This class is to store all error code constants which used in systems.

    @author          Zealot
    @company         DCIVision Limited
    @creation date   1/13/2005
    @version         $Revision: 1.2 $
*/

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.jgraph.graph.DefaultEdge;

import com.dcivision.workflow.applet.AppletResource;
import com.dcivision.workflow.applet.ListObject;
import com.dcivision.workflow.applet.WindowUtilities;
import com.dcivision.workflow.applet.net.HttpOperation;
import com.dcivision.workflow.applet.net.Utility;
import com.dcivision.workflow.bean.MtmWfStepNotificationRule;
import com.dcivision.workflow.bean.MtmWorkflowProgressPriorityRule;

public class NotificationRule extends JDialog {

  public static final String REVISION = "$Revision: 1.2 $";
  JPanel panel1 = new JPanel();
  JButton jButton1 = new JButton();
  DefaultEdge edge = null;
  String[] routeLable = null;
  JLabel jLabel2 = new JLabel();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JLabel jLabel5 = new JLabel();
  JComboBox jCBEscalateTrigger = new JComboBox();
  JTextField jTFEscalateAtAmt = new JTextField();
  JComboBox jCBEscalateAtField = new JComboBox();
  JCheckBox jCBEmail = new JCheckBox();
  JCheckBox jCBSystem = new JCheckBox();
  JButton jButton2 = new JButton();
  JScrollPane jScrollPane1 = new JScrollPane();
  JLabel jLabel6 = new JLabel();
  JList jLRule = new JList();
  DefaultListModel listRuleModel = new DefaultListModel();
  JButton jButton3 = new JButton();
  HttpOperation httpOperation = null;
  String notificationRuleListValue = null;
  StepInformationWindow stepInformationWindow=null;
  JButton jButton4 = new JButton();
  public NotificationRule(HttpOperation httpOperation,String notificationRuleListValue,StepInformationWindow stepInformationWindow, String title, boolean modal) {
    super(stepInformationWindow, title, modal);
    try {
      this.stepInformationWindow = stepInformationWindow;
      this.httpOperation = httpOperation;
      this.notificationRuleListValue=notificationRuleListValue;
      jbInit();
      pack();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    panel1.setLayout(null);
    jButton1.setBounds(new Rectangle(224, 249, 84, 25));
    jButton1.setText(AppletResource.lableArr[24]);
    jButton1.addActionListener(new NotificationRule_jButton1_actionAdapter(this));

    jLabel2.setText(AppletResource.lableArr[164]);
    jLabel2.setBounds(new Rectangle(27, 54, 103, 24));
    jLabel1.setText(AppletResource.lableArr[163]);
    jLabel1.setBounds(new Rectangle(30, 22, 307, 19));
    jLabel3.setText(AppletResource.lableArr[165]);
    jLabel3.setBounds(new Rectangle(174, 54, 107, 22));
    jLabel4.setText(AppletResource.lableArr[187]);
    jLabel4.setBounds(new Rectangle(285, 54, 96, 20));
    jLabel5.setText(AppletResource.lableArr[167]);
    jLabel5.setBounds(new Rectangle(380, 52, 91, 24));
    jCBEscalateTrigger.setBounds(new Rectangle(26, 87, 137, 22));
    jTFEscalateAtAmt.setText("0");
    jTFEscalateAtAmt.setBounds(new Rectangle(176, 87, 32, 22));
    jCBEscalateAtField.setBounds(new Rectangle(210, 87, 74, 20));
    jCBEmail.setText(AppletResource.lableArr[173]);
    jCBEmail.setBounds(new Rectangle(380, 83, 69, 21));
    jCBSystem.setText(AppletResource.lableArr[174]);
    jCBSystem.setBounds(new Rectangle(456, 81, 78, 25));
    jButton2.setBounds(new Rectangle(536, 83, 55, 22));
    jButton2.setText(AppletResource.lableArr[5]);
    jButton2.addActionListener(new NotificationRule_jButton2_actionAdapter(this));
    jScrollPane1.setBounds(new Rectangle(29, 153, 491, 86));
    jLabel6.setText(AppletResource.lableArr[198]);
    jLabel6.setBounds(new Rectangle(29, 128, 332, 22));
    jButton3.setBounds(new Rectangle(521, 213, 84, 22));
    jButton3.setSelected(false);
    jButton3.setText(AppletResource.lableArr[6]);
    jButton3.addActionListener(new NotificationRule_jButton3_actionAdapter(this));
    jButton4.setBounds(new Rectangle(291, 84, 81, 24));
    jButton4.setText(AppletResource.lableArr[187]);
    jButton4.addActionListener(new NotificationRule_jButton4_actionAdapter(this));
    getContentPane().add(panel1);
    panel1.add(jButton1, null);
    panel1.add(jLabel1, null);
    panel1.add(jLabel2, null);
    panel1.add(jLabel3, null);
    panel1.add(jCBEscalateTrigger, null);
    panel1.add(jLabel4, null);
    panel1.add(jTFEscalateAtAmt, null);
    panel1.add(jCBEscalateAtField, null);
    panel1.add(jCBEmail, null);
    panel1.add(jLabel5, null);
    panel1.add(jCBSystem, null);
    panel1.add(jButton2, null);
    panel1.add(jScrollPane1, null);
    jScrollPane1.getViewport().add(jLRule, null);
    panel1.add(jLabel6, null);
    panel1.add(jButton3, null);
    panel1.add(jButton4, null);
    initList();
  }

  public void initList(){
    String[][] escalateTriggerArr = {
        {AppletResource.lableArr[189], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_ARRIVED},
        {AppletResource.lableArr[190], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_PENDING_FOR},
        {AppletResource.lableArr[191], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_PRIORITY_CHANGED},
        {AppletResource.lableArr[192], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_CHECK_OUT_BY_USER},
        {AppletResource.lableArr[193], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_COMPLETED_BY_USER},
        {AppletResource.lableArr[194], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_COMPLETED},
        {AppletResource.lableArr[195], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_STOPPED},
        {AppletResource.lableArr[196], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_BEFORE_DUE_DATE},
        {AppletResource.lableArr[197], MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_OVER_DUE_DATE}
    };
    for(int i=0;i<escalateTriggerArr.length;i++){
      ListObject listObject = new ListObject();
      listObject.setID(escalateTriggerArr[i][1]);
      listObject.setName(escalateTriggerArr[i][0]);
      jCBEscalateTrigger.addItem(listObject);
    }
    String[][] escalateAtFieldArr = {{Calendar.DATE+"",AppletResource.lableArr[170]},
        {Calendar.HOUR+"",AppletResource.lableArr[171]},
        {Calendar.MINUTE+"",AppletResource.lableArr[172]}
    };
    for(int i=0;i<escalateAtFieldArr.length;i++){
      ListObject listObject = new ListObject();
      listObject.setID(escalateAtFieldArr[i][0]);
      listObject.setName(escalateAtFieldArr[i][1]);
      jCBEscalateAtField.addItem(listObject);
    }
    
    jLRule.setModel(listRuleModel);
    addRuleByRuleItem(notificationRuleListValue);

  }

  public void addRuleByRuleItem(String notificationRuleListValue){
    try{
      if(notificationRuleListValue==null){
        return;
      }
      String[] ruleList = WindowUtilities.splitString(notificationRuleListValue,"|");
      for(int i=0;i<ruleList.length;i++){
        String[] ruleItem = WindowUtilities.splitString(ruleList[i],",");
        String notificationRule="";
        if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_ARRIVED.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[168];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_PENDING_FOR.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[180];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_PRIORITY_CHANGED.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[181];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_CHECK_OUT_BY_USER.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[182];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_COMPLETED_BY_USER.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[183];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_COMPLETED.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[184];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_STOPPED.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[185];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_BEFORE_DUE_DATE.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[169];
        }else if (MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_OVER_DUE_DATE.equals(ruleItem[0])){
          notificationRule = AppletResource.lableArr[186];
        }
        notificationRule+=" "+ruleItem[1]+ " ";        
        if(Calendar.DATE == Integer.parseInt(ruleItem[2])) {
          notificationRule+=AppletResource.lableArr[170];
        }else if (Calendar.HOUR == Integer.parseInt(ruleItem[2])){
          notificationRule+=AppletResource.lableArr[171];
        }else{
          notificationRule+=AppletResource.lableArr[172];
        }
        String[] owners = WindowUtilities.splitString(ruleItem[5],":");
        String ownerStr ="";
        for(int j=0;j<owners.length;j++){
          String ownerName =  WindowUtilities.splitString(owners[j],"$")[1];
          if(!Utility.isEmpty(ownerStr)){
            ownerStr+=",";
          }
          ownerStr+=ownerName;
        }
        notificationRule+=", "+AppletResource.lableArr[188]+" "+ownerStr;
       
        if(!"null".equals(ruleItem[3]) || !"null".equals(ruleItem[4]) ){
          notificationRule+=", "+AppletResource.lableArr[167]+" ";
          if(!"null".equals(ruleItem[3])){
            notificationRule+=AppletResource.lableArr[173];
          }
          if(!"null".equals(ruleItem[4])){
            if(!"null".equals(ruleItem[3])){
              notificationRule+=" "+AppletResource.lableArr[176]+" ";
            }
            notificationRule+=AppletResource.lableArr[174];
          }
        }
        ListObject listObject = new ListObject();
        listObject.setID(ruleList[i]);
        listObject.setName(notificationRule);
        listRuleModel.addElement(listObject);

      }
    }catch(Exception ex){
    }
  }

  public void initData(){
    for(int i=0;i<routeLable.length;i++){
      ListObject listObject = new ListObject();
      listObject.setID(i+"");
      listObject.setName(routeLable[i]);
      //jCBRoute.addItem(listObject);

    }
  }
  public DefaultEdge getEdge(){
    return this.edge;
  }

  public void setEdge(DefaultEdge edge){
    this.edge = edge;
  }

  public void setrouteLable(String[] routeLable){
    this.routeLable = routeLable;
  }

  void jButton1_actionPerformed(ActionEvent e) {
    notificationRuleListValue="";
    int length = listRuleModel.getSize();
    for(int i=0;i<length;i++){
      ListObject listObject=(ListObject)listRuleModel.getElementAt(i);
      if(i==0){
        notificationRuleListValue+=listObject.getID();
      } else {
        notificationRuleListValue+="|"+listObject.getID();
      }
    }
    stepInformationWindow.notificationRuleListValue=notificationRuleListValue;
    this.dispose();
  }

  void setHttpOperation(HttpOperation httpOperation){
    this.httpOperation = httpOperation;
  }

  void jButton2_actionPerformed(ActionEvent e) {
    try{
/*      String notificationRule="";
      notificationRule+=jTFEscalateAtAmt.getText()+" ";
      notificationRule+=((ListObject)jCBEscalateAtField.getSelectedItem()).getName()+" ";
      notificationRule+=((ListObject)jCBEscalateTrigger.getSelectedItem()).getName()+", ";
      //WindowUtilities.getObjectFromServlet();
      String url = "/workflow/appletMiddleware?dataType=GET_RESOURCEBYCODE";
      String level = ((ListObject)jCBEscalateLevel.getSelectedItem()).getName();
      url+="&CODE="+AppletResource.messageCodeArr[8]+"&ARG1="+URLEncoder.encode(level,"UTF-8");
      notificationRule+=WindowUtilities.getObjectFromServlet(httpOperation,url)+", ";
      if(jCBEmail.isSelected()||jCBSystem.isSelected()){
        notificationRule+=AppletResource.lableArr[167]+" ";
        if(jCBEmail.isSelected()){
          notificationRule+=AppletResource.lableArr[173];
        }
        if(jCBSystem.isSelected()){
          notificationRule+=" "+AppletResource.lableArr[176]+" ";
          notificationRule+=AppletResource.lableArr[174];
        }
      }*/
      String notificationRuleValue="";
      if(Utility.isEmpty(jTFEscalateAtAmt.getText())){
        stepInformationWindow.getEditor().outputMessage(AppletResource.lableArr[178],true);
        return;
      }
      notificationRuleValue+=((ListObject)jCBEscalateTrigger.getSelectedItem()).getID()+",";
      notificationRuleValue+=jTFEscalateAtAmt.getText()+",";
      notificationRuleValue+=((ListObject)jCBEscalateAtField.getSelectedItem()).getID()+",";
      
      //notificationRuleValue+=((ListObject)jCBEscalateLevel.getSelectedItem()).getID()+",";
      if(jCBEmail.isSelected()){
        notificationRuleValue+=MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL+",";
      }else{
        notificationRuleValue+="null,";
      }
      if(jCBSystem.isSelected()){
        notificationRuleValue+=MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM+",";
      }else{
        notificationRuleValue+="null,";
      }
      notificationRuleValue+=stepInformationWindow.notificationOwner;
      addRuleByRuleItem(notificationRuleValue);
      //clear onwer
      stepInformationWindow.notificationOwner="";
    }catch(Exception ex){
    }


  }

  void jButton3_actionPerformed(ActionEvent e) {
    int index = jLRule.getSelectedIndex();
    listRuleModel.remove(index);

  }

  void jButton4_actionPerformed(ActionEvent e) {
    NotificationOwner NotificationOwner = new NotificationOwner(stepInformationWindow, "Notificaltion Owner", true);
    NotificationOwner.setBounds(this.getX(),this.getY(),630,320);
    NotificationOwner.setLocationRelativeTo(this);    
    NotificationOwner.show();
  }
}

class NotificationRule_jButton2_actionAdapter implements java.awt.event.ActionListener {
  NotificationRule adaptee;

  NotificationRule_jButton2_actionAdapter(NotificationRule adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class NotificationRule_jButton3_actionAdapter implements java.awt.event.ActionListener {
  NotificationRule adaptee;

  NotificationRule_jButton3_actionAdapter(NotificationRule adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

class NotificationRule_jButton1_actionAdapter implements java.awt.event.ActionListener {
  NotificationRule adaptee;

  NotificationRule_jButton1_actionAdapter(NotificationRule adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class NotificationRule_jButton4_actionAdapter implements java.awt.event.ActionListener {
  NotificationRule adaptee;

  NotificationRule_jButton4_actionAdapter(NotificationRule adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton4_actionPerformed(e);
  }
}
